package com.windy.common.transmit;

import cn.hutool.core.util.ObjectUtil;
import com.windy.common.context.HeaderContextHolder;
import com.windy.common.header.Headers;

import java.util.function.Supplier;

/**
 * Created by liyufeng on 2022/5/8
 */
public abstract class TransmitHeader<T> {
    private volatile Headers headers = new Headers();

    public final void transmitHeader(Supplier<T> supplier) {
        Headers pickerHeader = pickerHeader(supplier);
        this.headers= ObjectUtil.defaultIfNull(pickerHeader,headers);
        replayHeader(headers, supplier);
        storeHeader(headers);
    }

    protected void storeHeader(Headers headers) {
        HeaderContextHolder.setHeader(headers);
    }

    protected abstract void replayHeader(Headers headers, Supplier<T> supplier);

    protected abstract Headers pickerHeader(Supplier<T> supplier);


}
